package cn.tedu.hotel.serve.controller;

import cn.tedu.hotel.serve.pojo.dto.ThemeImgDTO;
import cn.tedu.hotel.serve.pojo.dto.ThemeImgUpdateDTO;
import cn.tedu.hotel.serve.pojo.entity.Theme;
import cn.tedu.hotel.serve.pojo.vo.ThemeImgVO;
import cn.tedu.hotel.serve.service.IThemeService;
import cn.tedu.hotel.serve.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/themes")
@Api(tags = "02. 主题住宿前后台功能")
@Validated
public class ThemeController {

    @Autowired
    IThemeService themeService;

    @GetMapping("/user/getThemeImgListByThemeNameSelectId")
    @ApiOperation("前台根据主题住宿名字查询主题住宿图片")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<ThemeImgVO>> getUserThemeImgList(String themeName){
        log.debug("开始处理【前台查询主题住宿图片】的请求,参数{}",themeName);
        List<ThemeImgVO> themeImgListByThemeNameSelectId = themeService.getThemeImgListByThemeNameSelectId(themeName);
        log.debug("数据：{}",themeImgListByThemeNameSelectId);
        return JsonResult.ok(themeImgListByThemeNameSelectId);
    }
    /*以下是后台管理的业务功能*/

    @PostMapping("/admin/addThemeImg")
    @ApiOperation("后台插入主题住宿图片")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 500)
    public JsonResult<Void> addThemeImg(@Validated ThemeImgDTO themeImgDTO){
        log.debug("开始处理【后台插入主题住宿图片】的请求");
       themeService.addThemeImg(themeImgDTO);
       return JsonResult.ok();
    }
    @PostMapping("/admin/{id:[0-9]+}/delete")
    @ApiOperation("后台删除主题住宿图片")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParam(name = "id", value = "主题相册ID", required = true, dataType = "long")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id) {
        log.debug("开始处理[根据ID后台删除主题住宿图片],参数:{}", id);
        themeService.delete(id);
        return JsonResult.ok();
    }
    @GetMapping("admin/themeName")
    @ApiOperation("后台返回主题住宿数据")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<Theme>> getTheme(){
        log.debug("开始处理【后台返回主题住宿数据】的请求");
        List<Theme> theme = themeService.getTheme();
        log.debug("数据：{}",theme);
        return JsonResult.ok(theme);
    }

    @GetMapping("/admin/themeImgList")
    @ApiOperation("后台查询主题住宿图片")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 801)
    public JsonResult<List<ThemeImgVO>> getAdminThemeImgList(){
        log.debug("开始处理【后台查询主题住宿图片】的请求");
        List<ThemeImgVO> themeImgList = themeService.getThemeImgList();
        log.debug("数据：{}",themeImgList);
        return JsonResult.ok(themeImgList);
    }

    @GetMapping("/admin/{id:[0-9]+}")
    @ApiOperation("后台查询主题住宿图片(单个)")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 802)
    public JsonResult<ThemeImgVO> getThemeImgVO(@PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id){
        ThemeImgVO themeImg = themeService.getThemeImgById(id);
        return JsonResult.ok(themeImg);
    }

    @PostMapping("/admin/{id:[0-9]+}/update")
    @ApiOperation("后台修改主题住宿数据")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 700)
    @ApiImplicitParam(name = "id", value = "主题相册ID", required = true, dataType = "long")
    public JsonResult<Void> update(@PathVariable @Range(min = 1, message = "请提交有效的ID值") Long id,
                                   @Validated ThemeImgUpdateDTO themeImgUpdateDTO) {
        log.debug("开始处理[根据ID后台修改主题住宿图片],参数:{}", id,themeImgUpdateDTO);
        themeService.update(id,themeImgUpdateDTO);
        return JsonResult.ok();
    }
}
